package com.g04.qt;

public class MyStack<T> {
    private Object[] stack;
    private int top;

    public MyStack(int size) {
        // 初始化栈
        this.stack = new Object[size];
        this.top = 0;
    }

    public boolean push(T c) {
        // 判断满了吗
        if (top == stack.length) {
            System.out.println("栈满，先扩容...");
            return false;
        }
        stack[top++] = c;
        return true;
    }

    public boolean isEmpty() {
        return top == 0;
    }

    public T peek() {
        // 查看栈顶元素
        return isEmpty() ? null : (T) stack[top - 1];
    }

    public T pop() {
        return isEmpty() ? null : (T) stack[--top];
    }

    public void addSpace(int size) {
        // 扩容 size 大小
        if (size > 0) {
            Object[] s1 = new Object[stack.length + size];
            for (int i = 0; i < top; i++) {
                s1[i] = stack[i];
            }
            stack = s1;
        } else {
            System.out.println("扩容空间必须大于 0");
        }
    }
}
